<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd()" v-hasPermi="['system:category:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" row-key="rid" :default-expand-all="isExpandAll" :tree-props="{children: 'categoryList', hasChildren: 'hasChildren'}">
      <el-table-column label="编号" align="center" prop="rid" width="100" />
      <el-table-column label="分类名称" align="center" prop="name" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <span v-if="row.status==='2'">启用</span>
          <span v-if="row.status==='1'">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="首页显示" align="center" prop="isHome">
        <template slot-scope="{ row }">
          <span v-if="row.isHome==='2'">显示</span>
          <span v-if="row.isHome==='1'">隐藏</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(row)" v-hasPermi="['system:category:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)" v-hasPermi="['system:category:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAdd(row)" v-hasPermi="['system:category:edit']">新增子级</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left" size="small">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="父级ID" prop="parentId">
          <md-select-tree v-model="form.parentId" :defaultExpandAll="true" :data="parentList" nodeKey="rid" :defaultProps="{label:'name',children:'categoryList'}" placeholder="请选择上级"></md-select-tree>
        </el-form-item>
        <el-form-item label="首页显示" prop="isHome">
          <el-radio-group v-model="form.isHome">
            <el-radio label="2">显示</el-radio>
            <el-radio label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="2">显示</el-radio>
            <el-radio label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCategory, getCategory, delCategory, addCategory, updateCategory } from "@/api/web/category";
export default {
  name: "Category",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分类表格数据
      categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 查询参数
      queryParams: {
        name: null,
        status: null,
        isHome: null,
        type: 2
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      parentList: []
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询分类列表 */
    getList () {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        this.categoryList = response.data;
        this.loading = false;
      });
    },
    /** 查询分类列表 */
    getParentList () {
      listCategory({
        type: 2
      }).then(response => {
        this.parentList = [];
        const menu = { rid: 0, name: '主类目', categoryList: [] };
        menu.categoryList = response.data
        this.parentList.push(menu)
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        rid: null,
        name: null,
        parentId: 0,
        remark: null,
        status: '2',
        isHome: '2',
        icon: null,
        type: 2,
        createTime: null,
        updateTime: null,
        deptId: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.rid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset();
      this.getParentList();
      this.open = true;
      this.title = "添加分类";
      if (row) {
        this.form.parentId = row.rid
      }
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.getParentList();
      const rid = row.rid || this.ids
      getCategory(rid).then(response => {
        this.form = response.data;

        this.open = true;
        this.title = "修改分类";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const params = Object.assign({}, { ...this.form })

          if (params.rid != null) {
            updateCategory(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCategory(params).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const rids = row.rid || this.ids;
      this.$modal.confirm('是否确认删除分类编号为"' + rids + '"的数据项？').then(function () {
        return delCategory(rids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('system/category/export', {
        ...this.queryParams
      }, `category_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
